package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTradeMarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTradeMarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTradeMarkService {

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;
    @Override
    public List<BaseTrademark> getTrademarkList(Long category3Id) {
        try {
            //  根据三级分类Id 查询品牌Id
            LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
            trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
            List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(trademarkLambdaQueryWrapper);

            //  判断
            if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
                //  这个集合中包含tmId;
                //            ArrayList<Long> tmIdList = new ArrayList<>();
                //            baseCategoryTrademarkList.forEach(baseCategoryTrademark -> {
                //                tmIdList.add(baseCategoryTrademark.getTrademarkId());
                //            });
                //  使用map 集合映射   R apply(T t);
                //            List<Long> tmIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                //                return baseCategoryTrademark.getTrademarkId();
                //            }).collect(Collectors.toList());
                List<Long> tmIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> baseCategoryTrademark.getTrademarkId()).collect(Collectors.toList());
                //  查询品牌集合列表。
                List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(tmIdList);
                //  返回数据
                return baseTrademarkList;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //  返回null
        return null;
    }

    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        //  根据三级分类Id 查询品牌Id
        //  select * from base_category_trademark where category3_id = 61 and is_deleted = 0;
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(trademarkLambdaQueryWrapper);
        //  判断 这个分类Id 与 品牌 是否进行绑定！
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //  获取以绑定的品牌Id
            //  1,2,3
            List<Long> tmIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> baseCategoryTrademark.getTrademarkId()).collect(Collectors.toList());
            //  查询品牌集合列表。
            /*
             baseTrademarkList select * from base_trademark where id in (1,2,3);
            1,小米,http://47.93.148.192:8080/group1/M00/01/71/rBHu8mEQpLiAP5NOAAAFtscZn_s397.png
            2,苹果,http://47.93.148.192:8080/group1/M00/01/71/rBHu8mEQpOuAXIroAAA8KOpezoQ651.png
            3,华为,http://47.93.148.192:8080/group1/M00/01/71/rBHu8mEQpUuAVioLAAGXnmYhX7M923.jpg
             */
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(tmIdList);

             /*
            查询所有品牌列表：baseTrademarkMapper.selectList(null) 1,2,3,5,6
            1,小米,http://47.93.148.192:8080/group1/M00/01/71/rBHu8mEQpLiAP5NOAAAFtscZn_s397.png
            2,苹果,http://47.93.148.192:8080/group1/M00/01/71/rBHu8mEQpOuAXIroAAA8KOpezoQ651.png
            3,华为,http://47.93.148.192:8080/group1/M00/01/71/rBHu8mEQpUuAVioLAAGXnmYhX7M923.jpg
            5,AA,http://47.93.148.192:9000/gmall/16312563680039569579c-dc7b-438d-9012-7f6a69532a66
            6,TT,http://192.168.200.129:9000/gmall/1631257139730ec6718d7-2ed2-46e5-8f5a-9af4b1e7a505
             */
            //  boolean test(T t);
            List<BaseTrademark> trademarkList = baseTrademarkMapper.selectList(null).stream().filter(baseTrademark -> {
                return !tmIdList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
            //  返回数据
            return trademarkList;
        }
        //  三级分类Id 与品牌没有绑定的时候;
        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //  先获取到品牌Id 列表; baseCategoryTrademark (61,5) (61,6)
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();

        //  声明一个集合来存储品牌与分类关系！
        //        ArrayList<BaseCategoryTrademark> baseCategoryTrademarkArrayList = new ArrayList<>();
        //        if(!CollectionUtils.isEmpty(trademarkIdList)){
        //            trademarkIdList.forEach(tmId->{
        //                //  将数据保存到 base_category_trademark 表;
        //                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
        //                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
        //                baseCategoryTrademark.setTrademarkId(tmId);
        //                //  循环执行insert 语句;
        //                //  baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
        //                baseCategoryTrademarkArrayList.add(baseCategoryTrademark);
        //            });
        //        }
        //  map 做关系映射：  R apply(T t);  stream(): --- 对集合或数组 中的元素 进行运算， 但是不会改变原有数据集. 保存运算之后的结果集，
        //  应该使用collect(Collectors.toList())
        List<BaseCategoryTrademark> baseCategoryTrademarkArrayList = trademarkIdList.stream().map(tmId -> {
            //  声明对象保存数据
            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
            baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
            baseCategoryTrademark.setTrademarkId(tmId);
            //  返回数据.
            return baseCategoryTrademark;
        }).collect(Collectors.toList());
        //  批量插入数据：base_category_trademark 让这个接口继承 IService
        //  this: 表示当前这个实现类的对象;
        this.saveBatch(baseCategoryTrademarkArrayList);
    }

    @Override
    public void remove(Long category3Id, Long trademarkId) {
        //  属于逻辑删除。
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        baseCategoryTrademarkMapper.delete(trademarkLambdaQueryWrapper);
    }
}
